package com.jy.importclient.common.util;

import com.jy.importclient.common.dto.VerifyFile;

import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @类名 StringUtil
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/4/24 17:35
 * @版本 1.0
 */
public class StringUtil {

    public static List<VerifyFile> toList(String context) {
        String[] fileNames = context.split("\n");
        VerifyFile verifyFile;
        List<VerifyFile> list = new ArrayList<>();
        for (String fileName : fileNames) {
            String[] elements = fileName.split("\\|");
            verifyFile = new VerifyFile();
            verifyFile.setDatFileName(elements[0]);
            verifyFile.setFileBytesSize(elements[1]);
            verifyFile.setFileRecords(elements[2]);
            list.add(verifyFile);
        }
        return list;
    }

    public static long countRecords(String context) {
        return context.split("\n").length;
    }

    public static long getFileSize(String filePath) throws IOException {
//        return context.getBytes(StandardCharsets.UTF_8).length;
        FileChannel channel = FileChannel.open(Paths.get(filePath));
        return channel.size();
    }

    public static String concat(StringBuffer stringBuffer, String... strings) {
        for (String string : strings) {
            stringBuffer.append(string);
        }
        return stringBuffer.toString();
    }

    public static String createFileName(String datFileName) {
        datFileName = datFileName.replace(".", "_");
        return datFileName + ".ctl";
    }
}
